using UnityEngine;


public static class TransformExtend
{
    #region 只设置Trans的某个属性值

    /// <summary>
    /// 只设置x
    /// </summary>
    /// <param name="trans"></param>
    /// <returns></returns>
    public static Vector3 SetLocalPosTion_X(this Transform trans, float x)
    {
        return trans.localPosition = new Vector3(x, trans.localPosition.y, trans.localPosition.z);
    }

    /// <summary>
    /// 只设置Y
    /// </summary>
    /// <param name="trans"></param>
    /// <returns></returns>
    public static Vector3 SetLocalPosTion_Y(this Transform trans, float y)
    {
        return trans.localPosition = new Vector3(trans.localPosition.x, y, trans.localPosition.z);
    }

    /// <summary>
    /// 只设置z
    /// </summary>
    /// <param name="trans"></param>
    /// <returns></returns>
    public static Vector3 SetLocalPosTion_Z(this Transform trans, float z)
    {
        return trans.localPosition = new Vector3(trans.localPosition.x, trans.localPosition.y, z);
    }

    /// <summary>
    /// 只设置x
    /// </summary>
    /// <param name="trans"></param>
    /// <returns></returns>
    public static Vector3 SetLocalRot_X(this Transform trans, float x)
    {
        return trans.eulerAngles = new Vector3(x, trans.eulerAngles.y, trans.eulerAngles.z);
    }

    /// <summary>
    /// 只设置Y
    /// </summary>
    /// <param name="trans"></param>
    /// <returns></returns>
    public static Vector3 SetLocalRot_Y(this Transform trans, float y)
    {
        return trans.eulerAngles = new Vector3(trans.eulerAngles.x, y, trans.eulerAngles.z);
    }

    /// <summary>
    /// 只设置z
    /// </summary>
    /// <param name="trans"></param>
    /// <returns></returns>
    public static Vector3 SetLocalRot_Z(this Transform trans, float z)
    {
        return trans.eulerAngles = new Vector3(trans.eulerAngles.x, trans.eulerAngles.y, z);
    }

    #endregion


    /// <summary>
    /// 删除所有子物体
    /// </summary>
    /// <param name="trans"></param>
    private static void ClearAllChild(this Transform trans)
    {
        for (int i = trans.childCount - 1; i >= 0; i--)
        {
            Object.DestroyImmediate(trans.GetChild(i).gameObject);
        }
    }
    
    /// <summary>
    /// 删除所有子物体
    /// </summary>
    /// <param name="trans"></param>
    public static void ClearAllChildTemp(this Transform trans)
    {
        for (int i = trans.childCount - 1; i >= 0; i--)
        {
            Object.DestroyImmediate(trans.GetChild(i).gameObject);
        }
    }


    /// <summary>
    /// 隐藏所有子物体
    /// </summary>
    /// <param name="trans"></param>
    public static void HideAllChild(this Transform trans)
    {
        for (int i = trans.childCount - 1; i >= 0; i--)
        {
            trans.GetChild(i).gameObject.SetActive(false);
        }
    }

    /// <summary>
    /// 隐藏倒数数量的子物体
    /// </summary>
    /// <param name="trans"></param>
    public static void HideChild(this Transform trans, int count)
    {
        for (int i = trans.childCount - 1; i >= trans.childCount - Mathf.Abs(count); i--)
        {
            trans.GetChild(i).gameObject.SetActive(false);
        }
    }

    /// <summary>
    /// 查找子物体（递归查找）  
    /// </summary> 
    /// <param name="trans">父物体</param>
    /// <param name="goName">子物体的名称</param>
    /// <returns>找到的相应子物体</returns>
    public static Transform FindOnChild(this Transform trans, string goName)
    {
        Transform child = trans.Find(goName);
        if (child != null)
            return child;

        Transform go = null;
        for (int i = 0; i < trans.childCount; i++)
        {
            child = trans.GetChild(i);
            go = FindOnChild(child, goName);
            if (go != null)
                return go;
        }

        return null;
    }

    /// <summary>
    /// 查找子物体（递归查找）  where T : UnityEngine.Object
    /// </summary> 
    /// <param name="trans">父物体</param>
    /// <param name="goName">子物体的名称</param>
    /// <returns>找到的相应子物体</returns>
    public static T FindOnChild<T>(this Transform trans, string goName) where T : UnityEngine.Object
    {
        Transform child = trans.Find(goName);
        if (child != null)
        {
            return child.GetComponent<T>();
        }

        Transform go = null;
        for (int i = 0; i < trans.childCount; i++)
        {
            child = trans.GetChild(i);
            go = FindOnChild(child, goName);
            if (go != null)
            {
                return go.GetComponent<T>();
            }
        }

        return null;
    }
    
    public static T GetComponentByPath<T>(this Transform gameObject, string path) where T : Object
    {
        Transform child = gameObject.Find(path);
        if (child != null)
        {
            return child.GetComponent<T>();
        }

        return null;
    }

    /// <summary>
    /// 显隐的扩展
    /// </summary>
    /// <param name="trans"></param>
    /// <param name="active"></param>
    public static void SetActive(this Transform trans, bool active)
    {
        var go = trans.gameObject;
        go.SetActive(active);
    }

    public static void Reset(this Transform tf)
    {
        tf.localPosition = Vector3.zero;
        tf.localRotation = Quaternion.identity;
        tf.localScale = Vector3.one;
    }
}